<?php


namespace App\Services;


use App\Models\HouseBroker;
use App\Tools\JsonTools;
use App\Tools\RedisTools;

class RankingServices
{

    /**
     * 经纪人排行榜
     * @param $id
     * @return array[]
     */

    public static function brokerRanking($id){

        $redis = RedisTools::getInstance()->connectRedis();
//        查询排行榜前10位
        $rankinglist = $redis->zRevRange('rankingList',0,9);

        $housebroker = [];
//        循环查询出对应的ID的头像和昵称
        foreach ($rankinglist as $k=>$v){

            $data = HouseBroker::where('id',$v)->first();

            $housebroker[] = [
              'id'=>$v,
              'broker_name'=>$data['broker_name'],
              'broker_image'=>$data['broker_image'],
              'rank'=>$k+1
            ];
            if ($v==$id){
                $myranking = [
                    'id'=>$v,
                    'broker_name'=>$data['broker_name'],
                    'broker_image'=>$data['broker_image'],
                    'rank'=>$k+1
                ];
            }
    }
//        返回排行榜数据和自己的头像和昵称
        $data = [
            'ranking'=>$housebroker,
            'my'=>$myranking
        ];

         return $data;
    }


    public static function renovateRanking(){

        $redis = RedisTools::getInstance()->connectRedis();

        $data = HouseBroker::get()->toArray();

        foreach ($data as $v){

            $redis->zAdd('rankingList',$v['sales_volume'],$v['id']);

        }


    }

}
